// import Link from './link'
// import View from './view'

let Vue = null

export default  class VueRouter {
    constructor (options) {
        // 保存路由选项
        this.$options = options
    //     this.routeMap = {};
    // this.data = _Vue.observable({
    //   current :"/"
    // })
        
        // Vue.util.defineReactive(this, 'matched', [])
        
        // window.addEventListener('hashchange', this.onHashChange())
        // window.addEventListener('load', this.onHashChange())
        // console.log(this.onHashChange,"ss11");
        // console.log(this.onHashChange,"ss22");
        // this.data = Vue.observable({
        //   current :""
        // })
    }
    
    onHashChange () {
        this.current = window.location.hash.slice(1) ? window.location.hash.slice(1):"/"
        console.log("this.current",this.current);
        this.matched = []
        this.match()
    }
    
    match (routes) {
        routes = routes || this.$options.routes
        for (let route of routes) {
            if (route.path === '/' && this.current === '/') {
                this.matched.push(route)
                return
            }            
            if (route.path !== '/' && this.current.indexOf(route.path) !== -1) {
                this.matched.push(route)
                if (route.children) {
                    this.match(route.children)
                }
                return
            }
        }
    }
}

// Vue插件需要声明一个install方法
VueRouter.install = function (vue) {
    Vue = vue
    Vue.mixin({
        beforeCreate() {
            if (this.$options.router) {
                Vue.prototype.$router = this.$options.router
            }
        }
    })
    // 注册全局组件
    Vue.component("router-link",{
      props:{
        to: {
          type: String,
          default: ''
        } 
      },
      // template:'<a :href="to"><slot></slot></a>'
      render(h){
        return h("a",{
          attrs:{
            href:'#'+this.to
          },
          
        },this.$slots.default)
      },
    })

    Vue.component("router-view",{
      render(h){
        const component =  self.routeMap[this.data.current]
        return h(component)
      }
    })
}


